package com.bort.system.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bort.system.common.DataContainer;
import com.bort.system.info.CheckOutResult;
import com.bort.system.info.DeskInfo;
import com.bort.system.info.LoginResult;
import com.bort.system.model.*;
import com.bort.system.service.ClientService;
import com.bort.system.utils.HttpClientUtils;
import com.bort.system.utils.Result;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.*;

public class ClientServiceImpl implements ClientService {

    private static ClientService clientService;

    public static ClientService getInstance(){
        if(clientService == null){
            clientService = new ClientServiceImpl();
        }
        return clientService;
    }

    public LoginResult login(String nameStr, String pdwStr) {
        LoginResult result = new LoginResult();
        result.setState("failed");
        if(StringUtils.isEmpty(nameStr)){
            result.setMsg("请输入用户名");
        }else if(StringUtils.isEmpty(pdwStr)){
            result.setMsg("请输入密码");
        }else {
            Map<String, String> map = new HashMap<>();
            map.put("userName",nameStr);
            map.put("password",pdwStr);
            String respStr = HttpClientUtils.sendFormPost(DataContainer.HOST_ADDRESS + "login", null, map);
            if (StringUtils.isNotEmpty(respStr)) {
                LoginResult loginRes = JSONObject.parseObject(respStr, LoginResult.class);
                return loginRes;
            } else {
                result.setMsg("网络异常");
            }
        }
        return result;
    }

    @Override
    public List<Goods> loadGoodsByCategoryId(Integer id) {
        Map<String,Object> map = new HashMap<>();
        map.put("categoryId",id);
        String resp = HttpClientUtils.getRequest(DataContainer.HOST_ADDRESS + "loadGoodsByCategoryId", map);
        if(StringUtils.isNotEmpty(resp)){
            Result result = JSONObject.parseObject(resp, Result.class);
            List<Goods> list = JSONArray.parseArray(JSONArray.toJSONString(result.getData()), Goods.class);
            return list;
        }
        return new ArrayList<>();
    }

    @Override
    public List<DeskInfo> getAllDesk() {
        String resStr = HttpClientUtils.httpGetParamsJson(DataContainer.HOST_ADDRESS + "getAllDesk", new HashMap<>());
        if(StringUtils.isNotEmpty(resStr)){
           Result<Object> result = JSONObject.parseObject(resStr,Result.class);
           if(result.getCode() == 200){
               List<DeskInfo> list = JSONArray.parseArray(JSONObject.toJSONString(result.getData()), DeskInfo.class);
               return list;
           }
        }
        return new ArrayList<>();
    }

    @Override
    public DeskInfo getDeskById(Integer id) {
        Map<String,Object> map = new HashMap<>();
        map.put("deskId",id);
        String respStr = HttpClientUtils.getRequest(DataContainer.HOST_ADDRESS + "getDeskById", map);
        if(StringUtils.isNotEmpty(respStr)){
            Result result = JSONObject.parseObject(respStr, Result.class);
            if(result.getCode() == 200){
                DeskInfo desk = JSONObject.parseObject(JSONObject.toJSONString(result.getData()), DeskInfo.class);
                return desk;
            }
        }
        return null;
    }

    @Override
    public Result<List<Category>> loadAllCategories(HashMap<Object, Object> objectObjectHashMap) {
        Result<List<Category>> result = new Result<>();
        String respStr = HttpClientUtils.httpGetParamsJson(DataContainer.HOST_ADDRESS + "loadCategories", new HashMap<>());
        if(StringUtils.isNotEmpty(respStr)){
           result = JSONObject.parseObject(respStr,Result.class);
           if(result.getCode() == 200){
               List<Category> list = JSONArray.parseArray(JSONObject.toJSONString(result.getData()), Category.class);
               result.setData(list);
           }
        }else{
            result.setData(new ArrayList<>());
        }
        return result;
    }

    @Override
    public TGameLog openDesk(DeskInfo data, Integer hValue, Integer mValue, String playerName) {
        Map<String,Object> params = new HashMap<>();
        params.put("data",data);
        params.put("hValue",hValue);
        params.put("mValue",mValue);
        params.put("playerName",playerName);
        String respStr = HttpClientUtils.sendPostWithJson(DataContainer.HOST_ADDRESS + "openDesk", getHeader(),params);
        if(StringUtils.isNotEmpty(respStr)){
            Result<TGameLog> result = JSONObject.parseObject(respStr, Result.class);
            if(result.getCode() == 200){
                return JSONObject.parseObject(JSONObject.toJSONString(result.getData()),TGameLog.class);
            }
        }
        return null;
    }

    @Override
    public CheckOutResult checkOut(DeskInfo deskInfo,Date endTime) {
        DeskInfo data = JSONObject.parseObject(JSONObject.toJSONString(deskInfo), DeskInfo.class);
        if(endTime != null){
            data.setEndTime(new Date());
        }
        Map<String,Object> params = new HashMap<>();
        params.put("data",data);
        String respStr = HttpClientUtils.sendPostWithJson(DataContainer.HOST_ADDRESS + "checkOut", getHeader(),params);
        if(StringUtils.isNotEmpty(respStr)){
           return JSONObject.parseObject(respStr,CheckOutResult.class);
        }
        return null;
    }

    @Override
    public Result sale(Goods indexGoods, String orderNo, int num) {
        Map<String,Object> params = new HashMap<>();
        params.put("indexGoods",indexGoods);
        params.put("orderNo",orderNo);
        params.put("num",num);
        String respStr = HttpClientUtils.sendPostWithJson(DataContainer.HOST_ADDRESS + "sale", getHeader(),params);
        if(StringUtils.isNotEmpty(respStr)){
            return JSONObject.parseObject(respStr,Result.class);
        }
        return null;
    }

    @Override
    public Result<List<Cart>> putInCart(Goods indexGoods, String orderNo, int num, Integer id, String playerName) {
        Map<String,Object> params = new HashMap<>();
        params.put("indexGoods",indexGoods);
        params.put("orderNo",orderNo);
        params.put("num",num);
        params.put("id",id);
        params.put("playerName",playerName);
        String respStr = HttpClientUtils.sendPostWithJson(DataContainer.HOST_ADDRESS + "putInCart", getHeader(),params);
        if(StringUtils.isNotEmpty(respStr)){
            return JSONObject.parseObject(respStr,Result.class);
        }
        return null;
    }

    @Override
    public Result removeCart(Cart cart) {
        Map<String,Object> params = new HashMap<>();
        params.put("cart",cart);
        String respStr = HttpClientUtils.sendPostWithJson(DataContainer.HOST_ADDRESS + "removeCart", getHeader(),params);
        if(StringUtils.isNotEmpty(respStr)){
            return JSONObject.parseObject(respStr,Result.class);
        }
        return null;
    }

    @Override
    public TGameLog addOvertime(DeskInfo data, Integer value, Integer mValue) {
        Map<String,Object> params = new HashMap<>();
        params.put("data",data);
        params.put("value",value);
        params.put("mValue",mValue);
        String respStr = HttpClientUtils.sendPostWithJson(DataContainer.HOST_ADDRESS + "addOvertime", getHeader(),params);
        if(StringUtils.isNotEmpty(respStr)){
            Result result = JSONObject.parseObject(respStr, Result.class);
            return JSONObject.parseObject(JSONObject.toJSONString(result.getData()),TGameLog.class);
        }
        return null;
    }

    @Override
    public boolean closeDesk(DeskInfo data, Date closedTime, BigDecimal amount, BigDecimal pay) {
        Map<String,Object> params = new HashMap<>();
        params.put("data",data);
        params.put("closedTime",closedTime);
        params.put("amount",amount);
        params.put("pay",pay);
        String respStr = HttpClientUtils.sendPostWithJson(DataContainer.HOST_ADDRESS + "closeDesk", getHeader(),params);
        if(StringUtils.isNotEmpty(respStr)){
           Result result = JSONObject.parseObject(respStr,Result.class);
           if(result.getCode() == 200){
               return true;
           }
        }
        return false;
    }

    private Map<String,String> getHeader(){
        Map<String,String> header = new HashMap<>();
        header.put("accessToken", DataContainer.loginUser.getAccessToken());
        String userIdStr = DataContainer.loginUser.getId() != null?DataContainer.loginUser.getId().toString():null;
        header.put("userId",userIdStr);
        return header;
    }
}
